home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / istream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  15.9 KB  |  575 lines

  1. #ifndef __STD_ISTREAM__
  2. #define __STD_ISTREAM__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * istream - Declarations for the Standard Library istreams
  8.  *
  9.  * $Id: istream,v 1.71 1996/09/24 19:17:11 smithey Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  14.  * ALL RIGHTS RESERVED *
  15.  * The software and information contained herein are proprietary to, and
  16.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  17.  * intends to preserve as trade secrets such software and information.
  18.  * This software is furnished pursuant to a written license agreement and
  19.  * may be used, copied, transmitted, and stored only in accordance with
  20.  * the terms of such license and with the inclusion of the above copyright
  21.  * notice.  This software and information or any other copies thereof may
  22.  * not be provided or otherwise made available to any other person.
  23.  *
  24.  * Notwithstanding any other lease or license that may pertain to, or
  25.  * accompany the delivery of, this computer software and information, the
  26.  * rights of the Government regarding its use, reproduction and disclosure
  27.  * are as set forth in Section 52.227-19 of the FARS Computer
  28.  * Software-Restricted Rights clause.
  29.  * 
  30.  * Use, duplication, or disclosure by the Government is subject to
  31.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  32.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  33.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  34.  * P.O. Box 2328, Corvallis, Oregon 97339.
  35.  *
  36.  * This computer software and information is distributed with "restricted
  37.  * rights."  Use, duplication or disclosure is subject to restrictions as
  38.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  39.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  40.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  41.  * then the "Alternate III" clause applies.
  42.  *
  43.  **************************************************************************/
  44.  
  45. #ifndef __STD_RWCOMPILER_H__
  46. #include <stdcomp.h>
  47. #endif
  48.  
  49. #include <ios>
  50. #include <ostream>
  51.  
  52. #ifndef _RWSTD_NO_NAMESPACE
  53. namespace std {
  54. #endif
  55.   
  56. extern istream _RWSTDExport cin;
  57.  
  58. #ifndef _RWSTD_NO_WIDE_CHAR
  59. extern wistream _RWSTDExport wcin;
  60. #endif
  61.  
  62. template<class charT, class traits>
  63. class _RWSTDExportTemplate basic_istream : virtual public basic_ios<charT, traits> {
  64.  
  65. public:
  66.  
  67.     // for abbreviation
  68.  
  69.     typedef basic_istream<charT, traits>             istream_type;
  70.     typedef basic_ios<charT, traits>                 ios_type;
  71.     typedef basic_streambuf<charT, traits>           streambuf_type;
  72.  
  73.     typedef traits                      traits_type;
  74.     typedef charT                  char_type;
  75.     typedef _TYPENAME traits::int_type   int_type;
  76.     typedef _TYPENAME traits::pos_type   pos_type;
  77.     typedef _TYPENAME traits::off_type   off_type;
  78.     
  79.  
  80.  
  81.     _EXPLICIT basic_istream(basic_streambuf<charT, traits> *sb);
  82.     virtual ~basic_istream();
  83.  
  84.     class sentry 
  85.     {
  86.     public:
  87.  
  88.       inline _EXPLICIT sentry(basic_istream<charT,traits>& stream,
  89.                              bool noskipws = 0)
  90.       : stream_(stream)
  91.       {
  92.          #ifdef _RWSTD_MULTI_THREAD
  93.           #ifndef _RWSTD_NO_EXCEPTIONS
  94.            try {
  95.           #endif
  96.            if ( stream.rdbuf() )
  97.            {
  98.              _RWSTDGuard* tmp = new _RWSTDGuard(stream.rdbuf()->buffer_mutex_);
  99.              if ( tmp )
  100.               stream.istream_sentry_guard = tmp;
  101.              else
  102.               stream.istream_sentry_guard = 0;
  103.            }
  104.          #endif
  105.  
  106.         if(!(stream.good())) 
  107.         {   
  108.           stream.setstate(ios_base::failbit);
  109.           ok_ = false;
  110.         }
  111.         else
  112.         {
  113.           if(stream.tie())  
  114.             stream.tie()->flush();
  115.  
  116.           if(!noskipws && (stream.flags() & ios_base::skipws))  
  117.           {
  118.             int_type        c;
  119.  
  120.             #  ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  121.             const ctype<charT>& ct = use_facet< ctype<charT> >(stream.getloc());
  122.             #  else
  123.             const ctype<charT>& ct = use_facet(stream.getloc(),        (ctype<charT>*)0);
  124.             #  endif
  125.          
  126.             while((c = stream.rdbuf()->sgetc()),
  127.                 ( !traits::eq_int_type(c,traits::eof()) && ct.is(ct.space,c) )) 
  128.             {
  129.               stream.rdbuf()->snextc();
  130.             }
  131.  
  132.             if( traits::eq_int_type(c,traits::eof()) )
  133.             {
  134.               stream.setstate(ios_base::eofbit);
  135.             }
  136.           }
  137.  
  138.           if(!(stream.good())) 
  139.           { 
  140.             stream.setstate(ios_base::failbit);
  141.           }
  142.           else 
  143.             ok_ =true;
  144.         }
  145.  
  146.      #ifdef _RWSTD_MULTI_THREAD
  147.       #ifndef _RWSTD_NO_EXCEPTIONS
  148.        }
  149.        
  150.       catch(...)
  151.        {
  152.          if ( stream.istream_sentry_guard )
  153.           {
  154.             delete stream.istream_sentry_guard;
  155.             stream.istream_sentry_guard = 0;
  156.             throw;
  157.            }
  158.          }
  159.        #endif
  160.       #endif
  161.  
  162.       }
  163.  
  164.       ~sentry() { 
  165.                      #ifdef _RWSTD_MULTI_THREAD
  166.                        if ( stream_.istream_sentry_guard )
  167.                         {
  168.                           delete stream_.istream_sentry_guard;
  169.                           stream_.istream_sentry_guard = 0;
  170.                         }
  171.                      #endif
  172.                 }
  173.  
  174.       operator bool () { return ok_; }
  175.  
  176.     private:
  177.  
  178.       basic_istream<charT,traits>& stream_;
  179.       bool ok_;
  180.  
  181.     };
  182.  
  183.     istream_type& operator>>(istream_type& (*pf)(istream_type&));
  184.     istream_type& operator>>(ios_base& (*pf)(ios_base&));
  185.     istream_type& operator>>(ios_type& (*pf)(ios_type&));
  186.  
  187. #ifndef _RWSTD_NO_BOOL
  188.     istream_type& operator>>(bool& n);
  189. #endif
  190.     istream_type& operator>>(short& n);
  191.     istream_type& operator>>(unsigned short& n);
  192.     istream_type& operator>>(int& n);
  193.     istream_type& operator>>(unsigned int& n);
  194.     istream_type& operator>>(long& n);
  195.     istream_type& operator>>(unsigned long& n);
  196.     istream_type& operator>>(float& f);
  197.     istream_type& operator>>(double& f);
  198.     istream_type& operator>>(long double& f);
  199. #ifdef _RWSTD_LONG_LONG
  200.     istream_type& operator>>(_RWSTD_LONG_LONG& n);
  201. #endif
  202.     istream_type& operator>>(void*& p);
  203.  
  204.     istream_type& operator>>(streambuf_type& sb);
  205.     istream_type& operator>>(streambuf_type *sb);
  206.  
  207.  
  208.     int_type get()
  209.     {
  210.       ios_base::iostate err = 0;
  211.  
  212.       #ifndef _RWSTD_NO_EXCEPTIONS
  213.       try {
  214.       #endif 
  215.  
  216.       _TYPENAME basic_istream<charT, traits>::sentry ipfx(*this,1);
  217.  
  218.       chcount_ = 0;
  219.  
  220.       if(ipfx) { 
  221.         int_type      c = rdbuf()->sbumpc();
  222.  
  223.         if( traits::eq_int_type(c,traits::eof()) )  
  224.         {
  225.           err = ios_base::eofbit | ios_base::failbit;
  226.         }
  227.         else
  228.         {
  229.            chcount_ = 1;
  230.            return c;
  231.         }
  232.       }
  233.  
  234.       #ifndef _RWSTD_NO_EXCEPTIONS
  235.       }
  236.       #endif
  237.  
  238.       #ifndef _RWSTD_NO_EXCEPTIONS
  239.       catch(...)
  240.       {
  241.         bool flag = false;
  242.         try {
  243.                 this->setstate(ios_base::badbit);
  244.             }
  245.         catch( ios_base::failure ) { flag= true; }
  246.         if ( flag ) throw;
  247.       }
  248.       #endif
  249.  
  250.       if ( err ) this->setstate(err);
  251.  
  252.       return traits::eof();
  253.     }
  254.  
  255.  
  256.     istream_type& get(char_type *s, streamsize n, char_type delim);
  257.     istream_type& get(char_type *s, streamsize n)
  258.     { return get(s,n,widen('\n')); }
  259.  
  260.     istream_type& get(char_type& c);
  261.  
  262.     istream_type& get(streambuf_type& sb, char_type delim);
  263.     istream_type& get(streambuf_type& sb)
  264.     { return get(sb,widen('\n')); }
  265.  
  266.     istream_type& getline(char_type *s, streamsize n, char_type delim);
  267.     istream_type& getline(char_type *s, streamsize n)
  268.     { return getline(s,n,widen('\n')); }
  269.  
  270.     istream_type& ignore(streamsize n = 1, int_type delim = traits::eof());
  271.  
  272.     istream_type& read(char_type *s, streamsize n);
  273.  
  274.     streamsize readsome(char_type *s, streamsize n);
  275.  
  276.     int peek();
  277.       
  278.     pos_type tellg();
  279.  
  280.     istream_type& seekg(pos_type pos)
  281.     {
  282.       ios_base::iostate err = 0;
  283.  
  284.       #ifndef _RWSTD_NO_EXCEPTIONS
  285.       try {
  286.       #endif 
  287.  
  288.       if ( this->rdstate() & ios_base::eofbit )
  289.         clear( this->rdstate() & ~ios_base::eofbit );
  290.  
  291.       #ifdef _RWSTD_MULTI_THREAD
  292.       if ( rdbuf() )
  293.         _RWSTDGuard guard(this->rdbuf()->buffer_mutex_);
  294.       #endif 
  295.  
  296.       if ( !this->bad() )
  297.         if(rdbuf()->pubseekpos(pos, ios_base::in) == pos_type(off_type(-1)))
  298.           err = ios_base::failbit;
  299.  
  300.       #ifndef _RWSTD_NO_EXCEPTIONS
  301.       }
  302.       #endif
  303.  
  304.       #ifndef _RWSTD_NO_EXCEPTIONS
  305.       catch(...)
  306.       {
  307.         bool flag = false;
  308.         try {
  309.           this->setstate(ios_base::badbit);
  310.         }
  311.         catch( ios_base::failure ) { flag= true; }
  312.       if ( flag ) throw;
  313.       }
  314.       #endif
  315.  
  316.       if ( err ) this->setstate(err);
  317.  
  318.       return *this;
  319.       }
  320.  
  321.       int sync();
  322.  
  323.       #ifndef _RWSTD_NO_INT_TYPEDEF
  324.       istream_type& seekg(off_type, ios_base::seekdir);
  325.       #endif
  326.  
  327.       istream_type& putback(char_type c);
  328.  
  329.       istream_type& unget();
  330.  
  331.       streamsize gcount() const;
  332.  
  333.       #ifdef _RWSTD_MULTI_THREAD
  334.        _RWSTDGuard *istream_sentry_guard;
  335.       #endif
  336.     
  337.    protected:
  338.       basic_istream( );
  339.  
  340.    private:
  341.       streamsize       chcount_;
  342. };
  343.  
  344.  
  345. /*
  346.  *
  347.  *  Class basic_iostream
  348.  *
  349.  */
  350.  
  351. template<class charT, class traits>
  352. class _RWSTDExportTemplate basic_iostream : public basic_istream<charT, traits>,
  353. public basic_ostream<charT, traits> 
  354.  
  355. {
  356.  
  357.   public:
  358.  
  359.     _EXPLICIT basic_iostream(basic_streambuf<charT, traits> *sb);
  360.     virtual ~basic_iostream();
  361.  
  362.   protected:
  363.  
  364.     basic_iostream();
  365.  
  366. };
  367.  
  368. //
  369. // istream iterator.
  370. //
  371.  
  372.  
  373. #ifdef _RWSTD_NO_UNDEFINED_FRIEND
  374. template <class T, class charT, class traits, class Distance>
  375. class _RWSTDExportTemplate istream_iterator;
  376.  
  377. template <class T, class charT, class traits, class Distance>
  378. bool _RWSTDExportTemplate operator== 
  379.                 (const istream_iterator<T,charT,traits,Distance>& x,
  380.                  const istream_iterator<T,charT,traits,Distance>& y);
  381.  
  382. #endif
  383.  
  384.  
  385. template <class T, class charT, class traits, class Distance>
  386. class _RWSTDExportTemplate istream_iterator : public iterator<input_iterator_tag,T,void>
  387. {
  388.   friend bool _RWSTDExportTemplate operator== (const istream_iterator<T,charT,traits,Distance>& x,
  389.                           const istream_iterator<T,charT,traits,Distance>& y);
  390.  
  391. protected:
  392.  
  393.     basic_istream<charT,traits>* stream;
  394.     T        value;
  395.     bool     end_marker;
  396.  
  397.     void read ()
  398.     {
  399.         end_marker = (*stream) ? true : false;
  400.         if (end_marker) *stream >> value;
  401.         end_marker = (*stream) ? true : false;
  402.     }
  403.  
  404. public:
  405.     typedef T value_type;
  406.     typedef charT char_type;
  407.     typedef traits traits_type;
  408.     typedef basic_istream<charT,traits> istream_type;
  409.  
  410.     istream_iterator () : stream(&cin), end_marker(false) {}
  411.     istream_iterator (basic_istream<charT,traits>& s) : stream(&s) { read(); }
  412.     istream_iterator ( const istream_iterator<T,charT,traits,Distance>& x )
  413.       : stream(x.stream) , value(x.value) , end_marker(x.end_marker)
  414.     { ; }
  415.     const T& operator* () const { return value; }
  416.     #ifndef _RWSTD_NO_NONCLASS_ARROW_RETURN
  417.     const T* operator->() const { return &value; }
  418.     #endif
  419.     istream_iterator<T,charT,traits,Distance>& operator++ ()
  420.     { 
  421.         read(); 
  422.         return *this;
  423.     }
  424.     istream_iterator<T,charT,traits,Distance> operator++ (int)
  425.     {
  426.         istream_iterator<T,charT,traits,Distance> tmp = *this;
  427.         read(); 
  428.         return tmp;
  429.     }
  430. };
  431.  
  432. template <class T, class charT, class traits, class Distance>
  433. inline bool _RWSTDExportTemplate  operator== 
  434.                        (const istream_iterator<T,charT,traits,Distance>& x,
  435.                         const istream_iterator<T,charT,traits,Distance>& y)
  436. {
  437.     return x.stream == y.stream && x.end_marker == y.end_marker ||
  438.            x.end_marker == false && y.end_marker == false;
  439. }
  440.  
  441. #ifndef _RWSTD_NO_NAMESPACE
  442. template <class T, class charT, class traits, class Distance>
  443. inline bool _RWSTDExportTemplate operator!= 
  444.                        (const istream_iterator<T,charT,traits,Distance>& x,
  445.                         const istream_iterator<T,charT,traits,Distance>& y)
  446. {
  447.     return !(x == y);
  448. }
  449. #endif
  450.  
  451.  
  452. template<class charT, class traits>
  453. basic_istream<charT, traits>&
  454. ws(basic_istream<charT, traits>& is);
  455.  
  456. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  457. typedef basic_istream<char>                               istream;
  458. #else
  459. typedef basic_istream<char, char_traits<char> >            istream;
  460. #endif
  461.  
  462. #ifndef _RWSTD_NO_WIDE_CHAR
  463. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  464. typedef basic_istream<wchar_t>                            wistream;
  465. #else
  466. typedef basic_istream<wchar_t, char_traits<wchar_t> >      wistream;
  467. #endif
  468. #endif
  469.  
  470. #ifndef _RWSTD_NO_DEFAULT_TEMPLATES
  471. typedef basic_iostream<char>                          iostream;
  472. #else
  473. typedef basic_iostream<char, char_traits<char> >       iostream;
  474. #endif
  475.  
  476. #ifndef _RWSTD_NO_WIDE_CHAR
  477. #ifndef _RWSTD_NO_DEFAULT_TEMPLATES
  478. typedef basic_iostream<wchar_t>                       wiostream;
  479. #else
  480. typedef basic_iostream<wchar_t, char_traits<wchar_t> > wiostream;
  481. #endif
  482. #endif
  483.  
  484.  
  485. // charT and charT* extractors
  486.  
  487. template<class charT, class traits>
  488. basic_istream<charT, traits>& _RWSTDExport 
  489. operator>> ( basic_istream<charT, traits>&, charT& );
  490.  
  491. template<class charT, class traits>
  492. basic_istream<charT, traits>& _RWSTDExport 
  493. operator>> ( basic_istream<charT, traits>&, charT* );
  494.  
  495.  
  496. // signed and unsigned extractors
  497.  
  498. #ifndef _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
  499. template <class traits>
  500. basic_istream<char, traits>& _RWSTDExport 
  501. operator>> ( basic_istream<char, traits>&, unsigned char& );
  502.  
  503. template <class traits>
  504. basic_istream<char, traits>& _RWSTDExport 
  505. operator>> ( basic_istream<char, traits>&, signed char& );
  506.  
  507. template <class traits>
  508. basic_istream<char, traits>& _RWSTDExport 
  509. operator>> ( basic_istream<char, traits>&, unsigned char* );
  510.  
  511. template <class traits>
  512. basic_istream<char, traits>& _RWSTDExport 
  513. operator>> ( basic_istream<char, traits>&, signed char* );
  514. #endif
  515.  
  516. // String extractor and getline
  517.  
  518. #ifndef _RWSTD_NO_NAMESPACE
  519. }
  520. namespace __rwstd {
  521. #endif
  522.  
  523. template <class streamT, class stringT, class traits>
  524. streamT& rw_extract_string(streamT& is, stringT& s, traits);
  525.  
  526. #ifndef _RWSTD_NO_NAMESPACE
  527. }
  528. namespace std {
  529. #endif
  530.  
  531. #ifndef _MSC_VER
  532. template<class charT, class traits, class Allocator>
  533. inline basic_istream<charT, traits>&
  534. _RWSTDExportTemplate operator>> (basic_istream<charT,traits>& is,
  535.             basic_string<charT,traits,Allocator>& s)
  536. {
  537.   return __RWSTD::rw_extract_string(is,s,traits());
  538. }
  539. #else
  540. inline istream& _RWSTDExport operator>> (istream& is, string& s)
  541. {
  542.   return __RWSTD::rw_extract_string(is,s,char_traits<char>());
  543. }
  544. #ifndef _RWSTD_NO_WIDE_CHAR
  545. inline wistream& _RWSTDExport operator>> (wistream& is, wstring& s)
  546. {
  547.   return __RWSTD::rw_extract_string(is,s,char_traits<wchar_t>());
  548. }
  549. #endif /* _RWSTD_NO_WIDE_CHAR */
  550. #endif /* _MSC_VER */
  551.  
  552. template<class charT, class traits, class Allocator>
  553. basic_istream<charT, traits>& _RWSTDExportTemplate
  554. getline( basic_istream<charT, traits>&,
  555.          basic_string<charT, traits, Allocator>&,
  556.          charT delim );
  557.  
  558. template<class charT, class traits, class Allocator>
  559. inline basic_istream<charT, traits>& _RWSTDExportTemplate
  560. getline( basic_istream<charT, traits>& is,
  561.          basic_string<charT, traits, Allocator>& str )
  562. { return getline(is,str,is.widen('\n')); }  
  563.  
  564.  
  565. #ifndef _RWSTD_NO_NAMESPACE
  566. }
  567. #endif
  568.  
  569. #ifdef _RWSTD_COMPILE_INSTANTIATE
  570. #include <istream.cc>
  571. #endif
  572.  
  573. #pragma option pop
  574. #endif /* __ISTREAM__ */
  575.